import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    menus: [],
  },
  mutations: {
    setMenus(state, menus) {
      state.menus = menus;
    },
  },
  actions: {
    async generateMenuData({ commit }, { appsPromse, routes }) {
      const routeMenuList = [];
      routes.forEach((v) => {
        if (!v.meta.hiddenMenu) {
          routeMenuList.push({
            url: v.path,
            title: v.meta.title,
            icon: v.meta.icon,
          });
        }
      });
      const apps = await appsPromse;
      const microAppMenuList = apps.map((v) => {
        if (v.container === "iframe") {
          const encodeUrl = window.btoa(v.entry); // 转码为base64
          return {
            url: `/iframe/${encodeUrl}`,
            title: v.name,
            icon: v.icon,
          };
        } else {
          return {
            url: Array.isArray(v.activeRule) ? v.activeRule[0] : v.activeRule,
            title: v.name,
            icon: v.icon,
          };
        }
      });
      commit("setMenus", [...routeMenuList, ...microAppMenuList]);
    },
  },
  modules: {},
});
